// manager/pages/knowledgelibrarydesc/index.js
import config from '../../../utils/config';
import { request } from '../../../utils/request';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseUrl: config.baseUrl,
        id: '',
        typeName: '',
        detail: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            typeName: options.typeName,
            id: options.id
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.findServeLibraryNewsInfo();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },
    
    /**
     * 获取详情
     */
    findServeLibraryNewsInfo(e) {
        request({
            url: `/ecard/serveLibrary/findServeLibraryNewsInfo`,
            // method: "GET",
            data: { id: this.data.id }
        }).then(res => {
            if(res.code == 0){
                let data = res.data;
                let libContent = "";
                if(data.libContent){
                    // 处理富文本图片不显示问题，在图片src中添加接口地址
                    libContent = data.libContent.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi,(match, capture) => {
                        let url = capture;
                        if(url.indexOf('http:') == -1 && url.indexOf('https:') == -1){
                            url = this.data.baseUrl + capture
                        }
                        return `<img src="${url}" style="max-width:100%;" alt=""/>`
                    })
                    // let htmlArr = html.split(/<video [^>]*src=['"]([^'"]+)[^>]*><\/video>/)
                    // data.htmlArr = htmlArr;
                    data.libContent = libContent;
                }
                wx.setNavigationBarTitle({
                    title: res.data.libTitle || "知识文库详情"
                })
                this.setData({
                    detail: res.data,
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },
})